package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.mongo.Friend;
import com.itheima.domain.mongo.UserLike;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.UserLikeService;
import com.itheima.vo.PageBeanVo;
import com.itheima.vo.UserLikeVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户喜欢
 * @Author: bug
 */
@DubboService
public class UserLikeServiceImpl implements UserLikeService {

    @Autowired
    private MongoTemplate mongoTemplate;

    @DubboReference
    private UserInfoService userInfoService;


    // 保存喜欢
    @Override
    public void save(UserLike userLike) {
        mongoTemplate.save(userLike);
    }

    // 移除喜欢
    @Override
    public void remove(Long userId, Long LikeUserId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(LikeUserId));
        mongoTemplate.remove(query, UserLike.class);
    }

    // 查询喜欢数
    @Override
    public Long findLoves(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), UserLike.class);
    }

    // 查询粉丝数
    @Override
    public Long findFans(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("likeUserId").is(userId)), UserLike.class);
    }


    // 查询互相喜欢
    @Override
    public Long findEachLoveCount(Long userId) {
        return mongoTemplate.count(Query.query(Criteria.where("userId").is(userId)), Friend.class);
    }

    // 分页查询互相喜欢
    @Override
    public PageBeanVo findEachLoveByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 创建查询条件
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        // 查询
        List<Friend> list = mongoTemplate.find(query, Friend.class);
        // 声明voList
        List<UserLikeVo> voList = new ArrayList<>();
        // 遍历
        if (CollectionUtil.isNotEmpty(list)) {
            for (Friend friend : list) {
                // 创建vo
                UserLikeVo vo = new UserLikeVo();
                vo.setUserInfo(userInfoService.findUserInfoById(friend.getFriendId()));
                vo.setAlreadyLove(true);
                // 添加进voList
                voList.add(vo);
            }
        }
        // 查询总数
        long total = mongoTemplate.count(query, Friend.class);

        // 封装并返回分页对象
        return new PageBeanVo(pageNum,pageSize,total,voList);
    }

    // 分页查询喜欢
    @Override
    public PageBeanVo findLoveByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 创建查询条件
        Query query = new Query(Criteria.where("userId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        // 查询
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        // 声明voList
        List<UserLikeVo> voList = new ArrayList<>();
        // 遍历
        if (CollectionUtil.isNotEmpty(list)) {
            for (UserLike userLike : list) {
                // 创建vo
                UserLikeVo vo = new UserLikeVo();
                vo.setUserInfo(userInfoService.findUserInfoById(userLike.getLikeUserId()));
                vo.setAlreadyLove(true);
                // 添加进voList
                voList.add(vo);
            }
        }
        // 查询总数
        long total = mongoTemplate.count(query, UserLike.class);

        // 封装并返回分页对象
        return new PageBeanVo(pageNum,pageSize,total,voList);
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    // 分页查询粉丝
    @Override
    public PageBeanVo findFanByPage(Long userId, Integer pageNum, Integer pageSize) {
        // 创建查询条件
        Query query = new Query(Criteria.where("likeUserId").is(userId))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum - 1) * pageSize).limit(pageSize);
        // 查询
        List<UserLike> list = mongoTemplate.find(query, UserLike.class);
        // 声明voList
        List<UserLikeVo> voList = new ArrayList<>();
        // 遍历
        if (CollectionUtil.isNotEmpty(list)) {
            for (UserLike userLike : list) {
                // 创建vo
                UserLikeVo vo = new UserLikeVo();
                vo.setUserInfo(userInfoService.findUserInfoById(userLike.getUserId()));
                if (stringRedisTemplate.hasKey(StrUtil.format("user_like:{}_{}", userId, userLike.getUserId()))) {
                    vo.setAlreadyLove(true);
                }
                // 添加进voList
                voList.add(vo);
            }
        }
        // 查询总数
        long total = mongoTemplate.count(query, UserLike.class);

        // 封装并返回分页对象
        return new PageBeanVo(pageNum,pageSize,total,voList);
    }

    // 查询是否喜欢
    @Override
    public Boolean findLove(Long userId,Long likeUserId) {
        // 创建查询条件
        Query query = new Query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
        // 查询
        UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
        // 判断非空
        return userLike != null;
    }
}
